package org.hong.samples.service.imp;

import java.util.ArrayList;
import java.util.List;

import org.hong.samples.dao.StudentMessageDAO;
import org.hong.samples.model.ExamScore;
import org.hong.samples.model.ExamSubject;
import org.hong.samples.model.UserJDBC;
import org.hong.samples.model.VO.ExamScoreVO;
import org.hong.samples.model.VO.ExamSubjectVO;
import org.hong.samples.model.VO.StudentsVO;
import org.hong.samples.model.VO.UserJDBCVO;
import org.hong.samples.service.StudentMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class StudentMessageServiceimp implements StudentMessageService {

	@Autowired
	private StudentMessageDAO studentMessageDAO;

	@Override
	public List<StudentsVO> findSubjectBysubId(String subId) {
		List<StudentsVO> student = new ArrayList<StudentsVO>();
		List<ExamScore> examScore = studentMessageDAO.findScoreBysubId(subId);
		for(ExamScore score: examScore){
			StudentsVO studentsVO = new StudentsVO();
			BeanUtils.copyProperties(score, studentsVO);
			UserJDBC user = studentMessageDAO.findById(score.getUserId());
				BeanUtils.copyProperties(user, studentsVO);
				ExamSubject sub = studentMessageDAO.findallBysubId(score.getSubjectId());
					BeanUtils.copyProperties(sub, studentsVO);
	student.add(studentsVO);
}
		return student;
	}

	@Override
	public List<ExamSubject> findAllSubject() {
		// TODO Auto-generated method stub
		return studentMessageDAO.findAllSubject();
	}
	@Override
	public List<StudentsVO> findAllBySubjectAndStudentName(String subId,String userName){
		List<StudentsVO> student = new ArrayList<StudentsVO>();
		List<ExamSubject> examSubject = studentMessageDAO.findSubjectBysubId(subId);
		List<UserJDBC> userJDBC = studentMessageDAO.findStudentByName(userName);
		for(ExamSubject sub:examSubject){
			StudentsVO studentsVO = new StudentsVO();
			ExamSubjectVO subsVO = new ExamSubjectVO();
			BeanUtils.copyProperties(sub, subsVO);
			BeanUtils.copyProperties(subsVO, studentsVO);
			for(UserJDBC user:userJDBC){
				UserJDBCVO userVO= new UserJDBCVO();
				BeanUtils.copyProperties(user, userVO);
				BeanUtils.copyProperties(userVO, studentsVO);
				List<ExamScore> examScore = studentMessageDAO.findScoreBysubIdAnduserId(subId, userVO.getUserId());
				for(ExamScore score: examScore){
					BeanUtils.copyProperties(score, studentsVO);
				}
			}
			student.add(studentsVO);
		}
		return student;
	}
	
	@Override
	public List<StudentsVO> findAllBySubjectAnduserId(String subId,String userId){
		List<StudentsVO> student = new ArrayList<StudentsVO>();
		List<ExamSubject> examSubject = studentMessageDAO.findSubjectBysubId(subId);
		List<UserJDBC> userJDBC = studentMessageDAO.findStudentById(userId);
		List<ExamScore> examScore = studentMessageDAO.findScoreBysubIdAnduserId(subId, userId);
		for(ExamSubject sub:examSubject){
			StudentsVO studentsVO = new StudentsVO();
			ExamSubjectVO subsVO = new ExamSubjectVO();
			BeanUtils.copyProperties(sub, subsVO);
			BeanUtils.copyProperties(subsVO, studentsVO);
			for(UserJDBC user:userJDBC){
				UserJDBCVO userVO= new UserJDBCVO();
				BeanUtils.copyProperties(user, userVO);
				BeanUtils.copyProperties(userVO, studentsVO);
				for(ExamScore score: examScore){
					BeanUtils.copyProperties(score, studentsVO);
				}
			}
			student.add(studentsVO);
		}
		return student;
	}
	
	@Override
	public List<StudentsVO> findAllBySubjectAnduserIdAnduserName(String subId,String userId,String userName){
		List<StudentsVO> student = new ArrayList<StudentsVO>();
		List<ExamSubject> examSubject = studentMessageDAO.findSubjectBysubId(subId);
		List<UserJDBC> userJDBC = studentMessageDAO.findStudentByNameAnduserId(userName, userId);
		List<ExamScore> examScore = studentMessageDAO.findScoreBysubIdAnduserId(subId, userId);
		for(ExamSubject sub:examSubject){
			StudentsVO studentsVO = new StudentsVO();
			ExamSubjectVO subsVO = new ExamSubjectVO();
			BeanUtils.copyProperties(sub, subsVO);
			BeanUtils.copyProperties(subsVO, studentsVO);
			for(UserJDBC user:userJDBC){
				UserJDBCVO userVO= new UserJDBCVO();
				BeanUtils.copyProperties(user, userVO);
				BeanUtils.copyProperties(userVO, studentsVO);
				for(ExamScore score: examScore){
					BeanUtils.copyProperties(score, studentsVO);
				}
			}
			student.add(studentsVO);
		}
		return student;
	}

}
